package com.example1.demo.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

import org.springframework.web.bind.annotation.PostMapping;

@RestController
public class upFile {

    private static final String UPLOAD_FOLDER = System.getProperty("user.dir");

    @PostMapping("/upload")
    public String upload(String nickname, MultipartFile f) throws IllegalStateException, IOException {

        System.out.println("文件大小：  " + f.getSize());
        System.out.println(f.getContentType());
        System.out.println(f.getOriginalFilename());
        System.out.println(UPLOAD_FOLDER);
        savaFile(f);
        return "上传成功";
    }

    private void savaFile(MultipartFile f) throws IllegalStateException, IOException {
        File upDir = new File(UPLOAD_FOLDER);
        if (!upDir.exists()) {
            upDir.mkdirs();
        }
        File file = new File(UPLOAD_FOLDER+f.getOriginalFilename());
        f.transferTo(file);
    }

}
